package com.mingqi.enetconnected.util.timer;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.date.DateUtils;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

/**
 * @author Administrator
 * @date 2017/7/18
 */

public class CountDownTimerUtils extends CountDownTimer {

    private TextView mTextView;

    public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);

        mTextView = textView;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setClickable(false); //设置不可点击

        String dateStr = DateUtils.parseLong2String(millisUntilFinished, DateUtils.msSDF);
        mTextView.setText(ResourceUtils.getString(R.string.remain) + " "+ dateStr);
        mTextView.setTextColor(ResourceUtils.getColor(R.color.count_down)); //设置按钮为灰色，这时是不能点击的

        SpannableString spannableString = new SpannableString(mTextView.getText().toString());  //获取按钮上的文字
        ForegroundColorSpan span = new ForegroundColorSpan(Color.BLACK);
        spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
        mTextView.setText(spannableString);
    }

    @Override
    public void onFinish() {

        mTextView.setText(ResourceUtils.getString(R.string.finished));
        mTextView.setClickable(true);//重新获得点击
        mTextView.setTextColor(Color.GRAY);  //还原背景色
    }


}
